//
//  SourceListDetailVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/14.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SourceListDetailVC.h"
#import "SourceListDetailCell.h"
#import "SourceListDetaiInfoCell.h"
#import "SourceLisLineCell.h"
//#import "SourceListModel.h"
#import "SourceListDetailModel.h"
#import "XLImageViewer.h"

@interface SourceListDetailVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView *header;
}
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) SourceListDetailCell *sourceListDetailCell;
@property (nonatomic,strong) SourceListDetaiInfoCell *sourceListDetaiInfoCell;
@property (nonatomic,strong) SourceLisLineCell *sourceLisLineCell;

@property (copy, nonatomic) NSDictionary *dicData;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSMutableArray *modelDetail;
// headerView
@property (nonatomic,strong) CJLabel *labOrderNum;
@property (nonatomic,strong) CJLabel *labShipperDetaiAddr;
@property (nonatomic,strong) CJLabel *labConsigneeDetailAddr;
@property (nonatomic,strong) CJLabel *labShipperName; // 发货人
@property (nonatomic,strong) CJLabel *labShipperPhone;
@property (nonatomic,strong) CJLabel *labConsigneeName;
@property (nonatomic,strong) CJLabel *labConsigneePhone;
//装卸货二维码背景view
@property (nonatomic,strong) UIView *viewloadAndUnloadQRBG;
// 装货
@property (nonatomic,strong) UIView *viewloadGoosBG; //装货
@property (nonatomic,copy) NSString *strOnShowLoadQR;  //0 隐藏  1展示
@property (nonatomic,strong) CJImageView *imgVloadGoodsLeft;
@property (nonatomic,strong) CJImageView *imgVloadGoods;
// 卸货
@property (nonatomic,strong) UIView *viewUnloadGoosBG; //装货
@property (nonatomic,copy) NSString *strOnShowUnloadQR;  //0 隐藏  1展示
@property (nonatomic,strong) CJImageView *imgVUnloadGoodsLeft;
@property (nonatomic,strong) CJImageView *imgVUnloadGoods;
@property (nonatomic,strong) UISwitch *switchInSure; // 是否显示价格
@property (nonatomic, strong) NSString *fareInfoDisplay;
@end

@implementation SourceListDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"订单详情")];
    [self setBackLeftButton:@""];
    
    self.sourceListDetailCell = [[SourceListDetailCell alloc]initWithFlex:nil reuseIdentifier:nil];
    self.sourceListDetaiInfoCell = [[SourceListDetaiInfoCell alloc]initWithFlex:nil reuseIdentifier:nil];
    self.sourceLisLineCell = [[SourceLisLineCell alloc]initWithFlex:nil reuseIdentifier:nil];

    self.strOnShowLoadQR = @"0";
    self.strOnShowUnloadQR = @"0";
   
    
    self.modelDetail = [[NSMutableArray alloc]init];
    self.modelArray = [[NSMutableArray alloc]init];
   
    [self.view addSubview:self.tableView];

    [self loadData];
}
-(void)switchIsAllowPriceAction:(UISwitch *)sender{
    if([sender isOn]){
        self.fareInfoDisplay = @"1";
    }else{
        self.fareInfoDisplay = @"0";
    }
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/order/updateOrderFare",CJBaseUrl);
    NSDictionary *dicPar = @{@"code":self.strCode,
                             @"fareInfoDisplay":self.fareInfoDisplay
    };

    [[HDNetworking sharedHDNetworking] PUT:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}
- (void)loadData{

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/order/%@",CJBaseUrl,self.strCode);
    WS(ws);
    NSDictionary *dic = [[NSDictionary alloc]init];

    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dic success:^(id  _Nonnull responseObject) {

        NSError * error ;
        if (self.modelDetail.count > 0) {
           [self.modelDetail removeAllObjects];
        }

        SourceListDetailModel *model = [[SourceListDetailModel alloc] initWithDictionary:(NSDictionary *)responseObject error:&error];
        [self.modelDetail addObject:model];

        //           DLog(@"++++++++%@",responseObject);
        ws.dicData = responseObject;
        self.fareInfoDisplay = model.fareInfoDisplay;
        [ws createTableView];

    } failure:^(NSError * _Nonnull error) {
    DLog(@"++++++++%@",error);

    }];
}

/// 创建tableview 等
- (void)createTableView{
    
    //    WS(ws);
    SourceListDetailModel *model = self.modelDetail[0];
    
    CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:[CJControl getUIWithNew] ? @"SourceListDetailHeaderViewNew":@"SourceListDetailHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;

    self.labOrderNum.text = model.mainOrderNumber;
    self.labShipperDetaiAddr.text = CJStringWithFormat(@"%@%@",model.shipperAddress,model.shipperDetailAddress);
    self.labConsigneeDetailAddr.text = CJStringWithFormat(@"%@%@",model.consigneeAddress,model.consigneeDetailAddress);
    self.labShipperName.text = model.shipperName;
    self.labShipperPhone.text = model.shipperPhone;
    self.labConsigneeName.text = model.consigneeName;
    self.labConsigneePhone.text = model.consigneePhone;
        
    if (model.qrCodeOnOff.intValue == 1) {
        self.viewloadAndUnloadQRBG.hidden = NO;
    }else{
        self.viewloadAndUnloadQRBG.hidden = YES;
    }
    [self.switchInSure addTarget:self action:@selector(switchIsAllowPriceAction:)forControlEvents:UIControlEventValueChanged | UIControlEventTouchDragExit];
    self.switchInSure.transform = CGAffineTransformMakeScale(0.75, 0.75);
    self.switchInSure.onTintColor = [UIColor cjMainColor];
    if ([self.fareInfoDisplay intValue] == 1) {
        self.switchInSure.on = YES;
    }else if([self.fareInfoDisplay intValue] == 0){
        self.switchInSure.on = NO;
    }
    self.viewloadGoosBG.hidden = YES;
    [self.imgVloadGoods sd_setImageWithURL:[NSURL URLWithString:[CJControl getThreeUnaryValueStr:model.loadingQr]] placeholderImage:[UIImage imageNamed:@"zcj_quesheng"]];
    self.viewUnloadGoosBG.hidden = YES;
    [self.imgVUnloadGoods sd_setImageWithURL:[NSURL URLWithString:[CJControl getThreeUnaryValueStr:model.unloadingQr]] placeholderImage:[UIImage imageNamed:@"zcj_quesheng"]];
    
    [self.modelArray removeAllObjects];
    
    // ,@"isDetail":@"1"   ,@"isDetail":@"0"  1 详情  0图标
    [self.modelArray addObject:@{@"title":@"zcj_icon_goodsInfo",@"content":CJLString(@"货物信息") ,@"isDetail":@"0"}];// 图标
    [self.modelArray addObject:@{@"title":CJLString(@"货物名称"),@"content":[CJRegular getIsStrEmpty:model.coalTypeName] ? @"":model.coalTypeName ,@"isDetail":@"1"}];
    
    if ([CJControl getIsTax]) {
            [self.modelArray addObject:@{@"title":CJLString(@"货物所属行业"),@"content":[CJRegular getIsStrEmpty:model.industryName] ? @"":model.industryName ,@"isDetail":@"1"}];
    }
    
    [self.modelArray addObject:@{@"title":CJLString(@"需求车数"),@"content":model.coalNumber == 0 ? @"不限":CJStringWithFormat(@"%@",@(model.coalNumber).cj_description) ,@"isDetail":@"1"}];
   
    if (model.notReceivedCount == 0) {
        [self.modelArray addObject:@{@"title":CJLString(@"未接车数"),@"content":@"不限" ,@"isDetail":@"1"}];
    }else{
        [self.modelArray addObject:@{@"title":CJLString(@"未接车数"),@"content":CJStringWithFormat(@"%@",@(model.notReceivedCount).cj_description) ,@"isDetail":@"1"}];
    }

    [self.modelArray addObject:@{@"title":@"货物数量",@"content":CJStringWithFormat(@"%@%@",@(model.weight).cj_description,[CJRegular getIsStrEmpty:model.coalUnitName] ? @"":model.coalUnitName),@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJStringWithFormat(@"未接数量"),@"content":CJStringWithFormat(@"%@%@",@(model.lastWeight).cj_description,[CJRegular getIsStrEmpty:model.coalUnitName] ? @"":model.coalUnitName),@"isDetail":@"1"}];
    
//    [self.modelArray addObject:@{@"title":CJLString(@"合理损耗"),@"content": model.limitWastageAmount  == 0 ? @"不计":CJStringWithFormat(@"%@%@",@(model.limitWastageAmount).cj_description ,model.limitWastageAmount == 0 ? @"":model.limitWastageTypeName),@"isDetail":@"1"}];
    
    if ([model.limitWastageTypeName isEqualToString:@"不计"]) {
        [self.modelArray addObject:@{@"title":CJLString(@"合理损耗"),@"content":@"不计",@"isDetail":@"1"}];

    }else{
        [self.modelArray addObject:@{@"title":CJLString(@"合理损耗"),@"content": CJStringWithFormat(@"%@%@",@(model.limitWastageAmount).cj_description ,model.limitWastageTypeName),@"isDetail":@"1"}];
    }
    
    
//    limitWastageTypeName
    [self.modelArray addObject:@{@"title":CJLString(@"装车时间"),@"content":[CJRegular getIsStrEmpty:model.lastLoadingTime] ? @"":model.lastLoadingTime ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"卸车时间"),@"content":[CJRegular getIsStrEmpty:model.lastUnloadingTime] ? @"":model.lastUnloadingTime ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"运输距离"),@"content":CJStringWithFormat(@"%@公里",@(model.distance).cj_description) ,@"isDetail":@"1"}];
    
    [self.modelArray addObject:@{@"title":@"zcj_feeDetail",@"content":CJLString(@"费用信息"),@"isDetail":@"0"}];//图标
    
    NSString *strCoalPriceName = @"";
    if (model.isTrunk == 3) {
        strCoalPriceName = CJStringWithFormat(@"元/车");
    }else{
        strCoalPriceName = CJStringWithFormat(@"元/%@",model.coalUnitName);
    }
    [self.modelArray addObject:@{@"title":@"货物单价",@"content":CJStringWithFormat(@"%@%@",@(model.coalPrice).cj_money_description,strCoalPriceName) ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"货物总价"),@"content":CJStringWithFormat(@"%@元",@(model.goodsTotal).cj_money_description) ,@"isDetail":@"1"}];
    // 投保时显示
    if (model.isInsure == 1) {
        [self.modelArray addObject:@{@"title":CJLString(@"投保金额"),@"content":CJStringWithFormat(@"%@元/车",@(model.insuranceFee).cj_money_description) ,@"isDetail":@"1"}];
    }
    
    [self.modelArray addObject:@{@"title":@"运费单价",@"content":CJStringWithFormat(@"%@%@",@(model.price).cj_money_description,[CJRegular getIsStrEmpty:model.isTrunkName] ? @"":model.isTrunkName)  ,@"isDetail":@"1"}];
    
    [self.modelArray addObject:@{@"title":@"运费含税单价",@"content":CJStringWithFormat(@"%@%@",@(model.taxPrice).cj_money_description,[CJRegular getIsStrEmpty:model.isTrunkName] ? @"":model.isTrunkName) ,@"isDetail":@"1"}];
    
    // 竞价是显示
    if (model.isQuote == 1) {
        [self.modelArray addObject:@{@"title":@"最高价",@"content":CJStringWithFormat(@"%@%@",@(model.maxPrice).cj_money_description,[CJRegular getIsStrEmpty:model.isTrunkName] ? @"":model.isTrunkName) ,@"isDetail":@"1"}];
        [self.modelArray addObject:@{@"title":@"最高含税价",@"content":CJStringWithFormat(@"%@%@",[CJRegular getIsStrEmpty:@(model.maxTaxPrice).cj_money_description] ? @"":@(model.maxTaxPrice).cj_money_description,[CJRegular getIsStrEmpty:model.isTrunkName] ? @"":model.isTrunkName)  ,@"isDetail":@"1"}];
    }
    
    [self.modelArray addObject:@{@"title":CJLString(@"运费总价"),@"content":CJStringWithFormat(@"%@元",@(model.allFee).cj_money_description),@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"运费含税总价"),@"content":CJStringWithFormat(@"%@元",@(model.taxFee).cj_money_description),@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"运费纳税金额"),@"content":CJStringWithFormat(@"%@元",@(model.taxPayFee).cj_money_description),@"isDetail":@"1"}];
    
    [self.modelArray addObject:@{@"title":CJLString(@"保费金额"),@"content":CJStringWithFormat(@"%@元/车",@(model.insuranceFee).cj_money_description),@"isDetail":@"1"}];
    
    [self.modelArray addObject:@{@"title":@"zcj_otherDetail",@"content":CJLString(@"其他信息") ,@"isDetail":@"0"}]; //图标
    
    [self.modelArray addObject:@{@"title":CJLString(@"指定司机"),@"content":[CJRegular getIsStrEmpty:model.pushTargetName] ? @"":model.pushTargetName ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"用车类型"),@"content":[CJRegular getIsStrEmpty:model.pushVehicleUse] ? @"":model.pushVehicleUse ,@"isDetail":@"1"}];
    
//    迭代去掉 21-08-13
//    [self.modelArray addObject:@{@"title":CJLString(@"开票方式"),@"content":[CJRegular getIsStrEmpty:model.billingTypeName] ? @"":model.billingTypeName ,@"isDetail":@"1"}];
    
    // 上游客户时显示
    if ([CJRegular getIsStrEmpty:model.upstreamName]) {
            
    }else{
        
        [self.modelArray addObject:@{@"title":CJLString(@"上游客户"),@"content":CJStringWithFormat(@"%@",model.upstreamName),@"isDetail":@"1" }];
        [self.modelArray addObject:@{@"title":CJLString(@"应收金额"),@"content":CJStringWithFormat(@"%@",@(model.inFee).cj_money_description),@"isDetail":@"1"}];
    }
    
    [self.modelArray addObject:@{@"title":CJLString(@"运输方式"),@"content":[CJRegular getIsStrEmpty:model.transportTypeName] ? @"":model.transportTypeName ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"运输要求"),@"content":[CJRegular getIsStrEmpty:model.coalName] ? @"暂无":model.coalName ,@"isDetail":@"1"}];
    [self.modelArray addObject:@{@"title":CJLString(@"是否常用货源"),@"content":model.isOftenOrder == 1 ? @"是":@"否" ,@"isDetail":@"1"}];
   
    [header layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *dic = self.modelArray[indexPath.row];
    NSString *isDetail = [CJControl getThreeUnaryValueStr:dic[@"isDetail"]];
    
    if ([isDetail isEqualToString:@"0"]) {
        static NSString *identifier = @"SourceListDetailCell";
        SourceListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SourceListDetailCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setCellInfoWithDict:self.modelArray[indexPath.row]];
    
        return cell;
    }
//    else if (indexPath.row == 10 || indexPath.row == 13){
//
//
//        static NSString *identifier = @"SourceLisLineCell";
//        SourceLisLineCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//        if (cell == nil) {
//            cell = [[SourceLisLineCell alloc] initWithFlex:nil reuseIdentifier:identifier];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//        }
//
////        [cell setCellInfoWithDict:self.modelArray[indexPath.row]];
//
//        return cell;
//
//    }
    else{
        static NSString *identifier = @"SourceListDetaiInfoCell";
        SourceListDetaiInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SourceListDetaiInfoCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        [cell setCellInfoWithDict:self.modelArray[indexPath.row]];
        return cell;
    }
    return nil;
}

#pragma mark ----------点击事件
- (void)touchViewHideOrShowLoadQRAction{
    
    CGAffineTransform transform = self.imgVloadGoodsLeft.transform;
    
    if (self.strOnShowLoadQR.intValue == 0) {
        self.viewloadGoosBG.hidden = NO;
        self.strOnShowLoadQR = @"1";
        transform = CGAffineTransformRotate(transform, M_PI/2);

    }else{
        self.viewloadGoosBG.hidden = YES;
        self.strOnShowLoadQR = @"0";
        transform = CGAffineTransformRotate(transform, -M_PI/2);
    }
    self.imgVloadGoodsLeft.transform = transform;
//    [self.tableView reloadData];
    
    [header layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];

}

//装货二维码
- (void)touchViewUpLoadQAction{
    SourceListDetailModel *model = self.modelDetail[0];
        [[XLImageViewer shareInstanse]showNetImages:@[[CJControl getThreeUnaryValueStr:model.loadingQr]] index:0 fromImageContainer:self.imgVloadGoods];
}


- (void)touchViewHideOrShowUnloadQRAction{
    
    CGAffineTransform transform = self.imgVUnloadGoodsLeft.transform;

    if (self.strOnShowUnloadQR.intValue == 0) {
        self.viewUnloadGoosBG.hidden = NO;
        self.strOnShowUnloadQR = @"1";
        transform = CGAffineTransformRotate(transform, M_PI/2);
    }else{
        self.viewUnloadGoosBG.hidden = YES;
        self.strOnShowUnloadQR = @"0";
        transform = CGAffineTransformRotate(transform, -M_PI/2);
    }
    self.imgVUnloadGoodsLeft.transform = transform;
    [header layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}


//卸货二维码
- (void)touchViewDownLoadQAction{
    SourceListDetailModel *model = self.modelDetail[0];
    [[XLImageViewer shareInstanse]showNetImages:@[[CJControl getThreeUnaryValueStr:model.unloadingQr]] index:0 fromImageContainer:self.imgVUnloadGoods];
}


- (CJTableView *)tableView{
    
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
//        WS(ws);
//        _tableView.blockMjHeader = ^(BOOL isheader) {
//
//            if (isheader) {
//                [ws downRefresh];
//            } else {
//                [ws upRefresh];
//            }
//
//        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
